import 'package:flutter/material.dart';
import 'package:pull_to_refresh/pull_to_refresh.dart';
import 'package:wanAndroid/bean/banner_model.dart';
import 'package:wanAndroid/bean/top_article_model.dart';
import 'package:wanAndroid/common/http/http_service.dart';
import 'package:wanAndroid/common/http/http_uts.dart';
import 'package:wanAndroid/common/uts/ui_uts.dart';
import 'package:wanAndroid/res/colors.dart';
import 'package:wanAndroid/widget/banner_widget.dart';
import 'package:wanAndroid/widget/top_article_item_view.dart';

class PageSy extends StatefulWidget {
  @override
  State<StatefulWidget> createState() => _PageSy();
}

class _PageSy extends State<PageSy> {
  RefreshController _refreshController;
  List<BannerData> bannerList = [];
  List<TopArticleData> topArticleList = [];

  @override
  void initState() {
    _refreshController = RefreshController();
    _initData();
    super.initState();
  }

  @override
  void dispose() {
    _refreshController.dispose();
    super.dispose();
  }

  ///初始化数据
  void _initData() async {
    getBanner();
    getTopArticle();
  }

  @override
  Widget build(BuildContext context) => Scaffold(
        backgroundColor: ResColors.scaffoldColor,
        appBar: UiUts.NoTitlebar(),
        body: SmartRefresher(
          scrollDirection: Axis.vertical,
          header: ClassicHeader(),
          enablePullUp: false,
          onRefresh: _initData,
          controller: _refreshController,
          child: topArticleList.isEmpty
              ? Center(
                  child: CircularProgressIndicator(
                    valueColor: AlwaysStoppedAnimation(
                      ResColors.primaryColor,
                    ),
                  ),
                )
              : ListView.builder(
                  itemBuilder: (context, pos) {
                    if (0 == pos) {
                      return BannerWidget(
                        bannerList,
                        height: 250,
                      );
                    } else {
                      TopArticleData data = topArticleList[pos - 1];
                      return TopArticleItemView(data);
                    }
                  },
                  itemCount: topArticleList.length + 1,
                ),
        ),
      );

  ///置顶文章
  Future<void> getTopArticle() {
    TopArticleModel topArticleModel;
    HttpService.topArticleList(
      HttpCallback((resp) {
        UiUts.finishRefresh(_refreshController);
        topArticleModel = TopArticleModel.fromJson(resp.data);
        if (HttpUts.isSuccess(topArticleModel.errorCode)) {
          setState(() {
            topArticleList = topArticleModel.data;
          });
        }
      }, (e) {
        UiUts.finishRefresh(_refreshController);
      }),
    );
  }

  ///获取banner
  Future<void> getBanner() {
    BannerModel banner;
    HttpService.bannerList(
      HttpCallback((resp) {
        UiUts.finishRefresh(_refreshController);
        banner = BannerModel.fromJson(resp.data);
        if (HttpUts.isSuccess(banner.errorCode)) {
          setState(() {
            bannerList = banner.data;
          });
        }
      }, (e) {
        UiUts.finishRefresh(_refreshController);
      }),
    );
  }

}
